<!--
Original JavaScript code by Ricardo Cabello (http://mrdoob.com/),
Joshua Koo (https://github.com/zz85/),
and Altered Qualia (https://github.com/alteredq/touchtoy).
Code reduction and WebVfx integration by Dan Dennedy (http://www.dennedy.org/).

The MIT License

Copyright &copy; 2010-2014 three.js authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <style>
        html, body {
            width: 100%;
            height: 100%;
            margin: 0px;
            overflow: hidden;
        }
    </style>
</head>
<body>
	<script src="three.min.js"></script>
	<script src="GeometryUtils.js"></script>
	<script src="Detector.js"></script>
	<script src="fonts/gentilis_bold.typeface.js"></script>
	<script src="fonts/gentilis_regular.typeface.js"></script>
	<script src="fonts/optimer_bold.typeface.js"></script>
	<script src="fonts/optimer_regular.typeface.js"></script>
	<script src="fonts/helvetiker_bold.typeface.js"></script>
	<script src="fonts/helvetiker_regular.typeface.js"></script>
	<script src="fonts/liberation_sans_regular.js"></script>
	<script src="fonts/liberation_sans_bold.js"></script>
	<script src="fonts/liberation_serif_regular.js"></script>
	<script src="fonts/liberation_serif_bold.js"></script>

	<script>

	if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

	var container, hex, color;
	var camera, cameraTarget, scene, renderer, pointLight;
	var group, textMesh1, textGeo, material;

	var text = "three.js",

		height = 20,
		size = 70,

		curveSegments = 4,

		bevelThickness = 2,
		bevelSize = 1.5,
		bevelSegments = 3,
		bevelEnabled = true,

		font = "optimer",
		weight = "bold", // normal bold
		style = "normal"; // normal italic

	var textPosition = new THREE.Vector2( 0, 0 );

	init();
	webvfx.renderRequested.connect(window, render);
	webvfx.readyRender(true);

	function decimalToHex( d ) {

		var hex = Number( d ).toString( 16 );
		hex = "000000".substr( 0, 6 - hex.length ) + hex;
		return hex.toUpperCase();

	}

	function init() {

		container = document.createElement( 'div' );
		document.body.appendChild( container );

		// CAMERA

		camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
		camera.position.set( 0, 400, 700 );

		cameraTarget = new THREE.Vector3( 0, 150, 0 );
		camera.lookAt( cameraTarget );

		// SCENE

		scene = new THREE.Scene();

		// LIGHTS

		var dirLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
		dirLight.position.set( 0, 0, 1 ).normalize();
		scene.add( dirLight );

		pointLight = new THREE.PointLight( 0xffffff, 1.5 );
		pointLight.position.set( 0, 100, 90 );
		scene.add( pointLight );

		pointLight.color.setHSL( Math.random(), 1, 0.5 );
		hex = decimalToHex( pointLight.color.getHex() );

		material = new THREE.MeshFaceMaterial( [
			new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } ), // front
			new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } ) // side
		] );

		group = new THREE.Object3D();
		group.position.y = 100;

		scene.add( group );

		createText();

		// RENDERER

		renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
		renderer.setSize( window.innerWidth, window.innerHeight );

		container.appendChild( renderer.domElement );
	}

	function createText() {

		textGeo = new THREE.TextGeometry( text, {

			size: size,
			height: height,
			curveSegments: curveSegments,

			font: font,
			weight: weight,
			style: style,

			bevelThickness: bevelThickness,
			bevelSize: bevelSize,
			bevelEnabled: bevelEnabled,

			material: 0,
			extrudeMaterial: 1

		});

		textGeo.computeBoundingBox();
		textGeo.computeVertexNormals();

		// "fix" side normals by removing z-component of normals for side faces
		// (this doesn't work well for beveled geometry as then we lose nice curvature around z-axis)

		if ( ! bevelEnabled ) {

			var triangleAreaHeuristics = 0.1 * ( height * size );

			for ( var i = 0; i < textGeo.faces.length; i ++ ) {

				var face = textGeo.faces[ i ];

				if ( face.materialIndex == 1 ) {

					for ( var j = 0; j < face.vertexNormals.length; j ++ ) {

						face.vertexNormals[ j ].z = 0;
						face.vertexNormals[ j ].normalize();

					}

					var va = textGeo.vertices[ face.a ];
					var vb = textGeo.vertices[ face.b ];
					var vc = textGeo.vertices[ face.c ];

					var s = THREE.GeometryUtils.triangleArea( va, vb, vc );

					if ( s > triangleAreaHeuristics ) {

						for ( var j = 0; j < face.vertexNormals.length; j ++ ) {

							face.vertexNormals[ j ].copy( face.normal );

						}

					}

				}

			}

		}

		var centerOffset = -0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );
		baselineOffset = 0.2 * ( textGeo.boundingBox.max.y - textGeo.boundingBox.min.y );

		textMesh1 = new THREE.Mesh( textGeo, material );

		textMesh1.position.x = centerOffset + textPosition.x;
		textMesh1.position.y = baselineOffset + textPosition.y;
		textMesh1.position.z = 0;

		textMesh1.rotation.x = 0;
		textMesh1.rotation.y = Math.PI * 2;

		group.add( textMesh1 );

	}

	function refreshText() {

		group.remove( textMesh1 );

		if ( !text ) return;

		createText();

	}

	function render(time) {

		var color = webvfx.getStringParameter('color');
		if (color) {
			var decimalColor = parseInt(color.substring(1), 16);
			pointLight.color.setHex(decimalColor);
		}
		var myText = webvfx.getStringParameter('text');
		if (text !== myText) {
			text = myText;
			refreshText();
		}
		var myFont = webvfx.getStringParameter('font');
        if (myFont === 'droid sans')
            myFont = 'liberation sans';
        else if (myFont === 'droid serif')
            myFont = 'liberation serif';
		if (font !== myFont) {
			font = myFont;
			refreshText();
		}
		var myWeight = webvfx.getStringParameter('weight');
		if (weight !== myWeight) {
			weight = myWeight;
			refreshText();
		}
		if (bevelEnabled !== webvfx.getNumberParameter('bevel')) {
			bevelEnabled = webvfx.getNumberParameter('bevel');
			refreshText();
		}
		if (height !== webvfx.getNumberParameter('depth')) {
			height = webvfx.getNumberParameter('depth');
			refreshText();
		}
		if (size !== webvfx.getNumberParameter('size')) {
			size = webvfx.getNumberParameter('size');
			refreshText();
		}
		var horizontal = (webvfx.getNumberParameter('horizontal') - 0.5) * window.innerWidth * 0.5;
		if (textPosition.x !== horizontal) {
			textPosition.x = horizontal;
			refreshText();
		}
		var vertical = (0.5 - webvfx.getNumberParameter('vertical')) * window.innerHeight * 0.5;
		if (textPosition.y !== vertical) {
			textPosition.y = vertical;
			refreshText();
		}

		group.rotation.x = 2.0 * (webvfx.getNumberParameter('x_rotation') - 0.6);

		renderer.clear();
		renderer.render( scene, camera );

	}

	</script>
</body>
</html>
